﻿using StockTradTest.Data;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StockTradTest
{
    //数据库索引
    public static class Base
    {
        public static string Root
        {
            get
            {
                string value = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StockData");
                if (!Directory.Exists(value))
                {
                    Directory.CreateDirectory(value);
                }
                return value;
            }
        }
        private static string _currentcode = "";
        public static string CurrentCode
        {
            get
            {
                return _currentcode;
            }
            set
            {
                if(_currentcode != value)
                {
                    _currentcode = value;
                    if (!Directory.Exists(Path.Combine(Root, "stocks")))
                    {
                        Directory.CreateDirectory(Path.Combine(Root, "stocks"));
                    }
                    if (!Directory.Exists(Path.Combine(Root, "indicators")))
                    {
                        Directory.CreateDirectory(Path.Combine(Root, "indicators"));
                    }
                    _stockdatabase = new StockDatabase(Path.Combine(Root, string.Format("stocks/stocks{0}.db3", _currentcode)));
                    _indicatorDatabase = new IndicatorDatabase(Path.Combine(Root, string.Format("indicators/indicators{0}.db3", _currentcode)));
                }
            }
        }
        private static string _currentIndexcode = "";
        public static string CurrentIndexCode
        {
            get
            {
                return _currentIndexcode;
            }
            set
            {
                if (_currentIndexcode != value)
                {
                    _currentIndexcode = value;
                    if (!Directory.Exists(Path.Combine(Root, "indexs")))
                    {
                        Directory.CreateDirectory(Path.Combine(Root, "indexs"));
                    }

                    _indexdatabase = new IndexDatabase(Path.Combine(Root, string.Format("indexs/indexs{0}.db3", _currentIndexcode)));
                }
            }
        }

        private static IndexDatabase _indexdatabase;

        public static IndexDatabase IndexDatabase
        {
            get
            {
                if (_indexdatabase == null)
                {
                    if (_currentIndexcode == "")
                    {
                        return null;
                    }
                    _indexdatabase = new IndexDatabase(Path.Combine(Root, string.Format("indexs/indexs{0}.db3", _currentIndexcode)));
                }
                return _indexdatabase;
            }
        }
        private static StockDatabase _stockdatabase;

        public static StockDatabase StockDatabase
        {
            get
            {
                if (_stockdatabase == null)
                {
                    if(_currentcode == "")
                    {
                        return null;
                    }
                    _stockdatabase = new StockDatabase(Path.Combine(Root, string.Format("stocks/stocks{0}.db3", _currentcode)));
                }
                return _stockdatabase;
            }
        }

        private static IndicatorDatabase _indicatorDatabase;

        public static IndicatorDatabase IndicatorDatabase
        {
            get
            {
                if (_indicatorDatabase == null)
                {
                    if (_currentcode == "")
                    {
                        return null;
                    }
                    _indicatorDatabase = new IndicatorDatabase(Path.Combine(Root, string.Format("indicators/indicators{0}.db3", _currentcode)));
                }
                return _indicatorDatabase;
            }
        }

        private static StockInfoDataBase _stockInfodatabase;

        public static StockInfoDataBase StockInfoDatabase
        {
            get
            {
                if (_stockInfodatabase == null)
                {
                    _stockInfodatabase = new StockInfoDataBase(Path.Combine(Root, "stockInfo.db3"));
                }
                return _stockInfodatabase;
            }
        }

        private static MyAccountDatabase _accountDatabase;

        public static MyAccountDatabase AccountDatabase
        {
            get
            {
                if (_accountDatabase == null)
                {
                    _accountDatabase = new MyAccountDatabase(Path.Combine(Root, "myaccount.db3"));
                }
                return _accountDatabase;
            }
        }

        private static MyStockDatabase _myStockDatabase;

        public static MyStockDatabase MyStockDatabase
        {
            get
            {
                if (_myStockDatabase == null)
                {
                    _myStockDatabase = new MyStockDatabase(Path.Combine(Root, "mystocks.db3"));
                }
                return _myStockDatabase;
            }
        }



        private static InvestorDatabase _investorDatabase;
        public static InvestorDatabase InvestorDatabase
        {
            get
            {
                if (_investorDatabase == null)
                {
                    _investorDatabase = new InvestorDatabase(Path.Combine(Root, "Investor.db3"));
                   
                }
                return _investorDatabase;
            }
        }

    }


}
